
import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * main.java.JarFileUtil
 *
 * @author sunxh
 * @date 15/10/28
 */
public class JarFileUtil {

    private final static int BUFFER = 1024;

    public void copyFolderFromJar() throws IOException {

        final String path = "packBaseFolder";

        final String destBasePath = "/app/tmp/ceshidabao";

        final File jarFile = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getPath());

        if (jarFile.isFile()) {  // Run with JAR file
            final JarFile jar = new JarFile(jarFile);
            final Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
            while (entries.hasMoreElements()) {
                final String name = entries.nextElement().getName();
                if (name.startsWith(path + "/")&&name.contains(".")) { //filter according to the path
                    System.out.println(name);

                    final String destFilePath = destBasePath + File.separator + name;

                    InputStream inputStream = this.getClass().getResourceAsStream(name);

                    createFile(destFilePath);

                    File destFile = new File(destFilePath);

                    FileOutputStream out = null;
                    try {
                        out = new FileOutputStream(destFile);
                        byte[] b = new byte[BUFFER];
                        int len = 0;
                        while ((len = inputStream.read(b)) != -1) {
                            out.write(b, 0, len);
                        }
                        inputStream.close();
                        out.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                    System.out.println("copy file :" + destFilePath);

                }
            }
            jar.close();
        } else { // Run with IDE
            final URL url = this.getClass().getResource("/" + path);
            if (url != null) {
                try {
                    final File apps = new File(url.toURI());
                    for (File app : apps.listFiles()) {
                        System.out.println(app);
                    }
                } catch (URISyntaxException ex) {
                    // never happens
                }
            }
        }
    }

    public static boolean createFile(String destFileName) {
        File file = new File(destFileName);
        if(file.exists()) {
            System.out.println("创建单个文件" + destFileName + "失败，目标文件已存在！");
            return false;
        }
        if (destFileName.endsWith(File.separator)) {
            System.out.println("创建单个文件" + destFileName + "失败，目标文件不能为目录！");
            return false;
        }
        //判断目标文件所在的目录是否存在
        if(!file.getParentFile().exists()) {
            //如果目标文件所在的目录不存在，则创建父目录
            System.out.println("目标文件所在目录不存在，准备创建它！");
            if(!file.getParentFile().mkdirs()) {
                System.out.println("创建目标文件所在目录失败！");
                return false;
            }
        }
        //创建目标文件
        try {
            if (file.createNewFile()) {
                System.out.println("创建单个文件" + destFileName + "成功！");
                return true;
            } else {
                System.out.println("创建单个文件" + destFileName + "失败！");
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("创建单个文件" + destFileName + "失败！" + e.getMessage());
            return false;
        }
    }

}
